#pragma once

#include <Arduino.h>
#include "esp_http_client.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <SD_MMC.h>
#include <LinkedList.h>
// #include "https_download.h"

// 定义下载状态枚举
enum class DownloadStatus
{
    IDLE,
    DOWNLOADING,
    COMPLETED,
    FAILED
};

// 定义事件处理函数类型
typedef void (*DownloadEventCallback)(DownloadStatus status);

typedef struct
{
    String url;
    String filePath;
    char *certPem;
} HttpsDownloadInfo_t;

class HttpsDownload2
{
private:
    LinkedList<HttpsDownloadInfo_t> downloadQueue;
    TaskHandle_t downloadTaskHandle;
    DownloadEventCallback eventCallback;
    DownloadStatus status;
    size_t totalSize;
    size_t downloadedSize;
    TickType_t startTime;
    float averageSpeed;
    File file;
    
    esp_err_t performDownload();
    void updateStatus(DownloadStatus newStatus);
    static void downloadTask(void *pvParameters);
    static esp_err_t eventHandler(esp_http_client_event_t *evt);
    void updateSpeed();
public:
    HttpsDownload2();
    ~HttpsDownload2();
    void begin();
    void setEventCallback(DownloadEventCallback callback);
    void add(HttpsDownloadInfo_t &info);
    DownloadStatus getStatus();
    uint8_t getListSize();
    size_t getTotalSize();
    size_t getDownloadedSize();
    float getDownloadPercentage();
    float getAverageDownloadSpeed();
    void print();
    HttpsDownloadInfo_t info;

};
extern HttpsDownload2 downloader;